﻿(function ($) {

    app.CaptionInit = function(capid) {
        var reqData = {
            source: 'CarCatalogue',
            oper: 'last',
            amount: 20,
            sord: 'DESC'
        };

        $.post('callbacks.php', reqData,
                function (rsp, textStatus) {
					if( !rsp ) return;
					if (rsp.errcode) {
						if (rsp.errcode == '999') { 
							alert(rsp.errmess);
							app.customer = { uname:'', cust_id:0 };
							location.href = 'index.php'; 
						}
						else app.alert('AlertDlg', 'Ошибка', rsp.errmess, 'ipdepo-icon-stop32');
						return;
					}
					if( rsp.data.length )
						ShowCarousel(rsp.data);
                }, 
                'json'
        );

    function ShowCarousel(data) {
		var carousel_id = capid+'_carousel';
		var container = carousel_id+'_container';
		
		$('#'+capid).append(
			'<div style="text-shadow: 1px 1px #bbbbbb;font-size:14px;font-style:oblique;width:130px;margin:12px;">Наша двадцатка последних предложений</div>'+
			'<div id="'+container+'" style="margin:2px;position:absolute;top:0;left:150px;bottom:0;right:0;"></div>'
		);
		var ul = '<ul id="'+carousel_id+'" class="jcarousel-skin-tango">';
		for( var i=0; i<data.length; i++ ) {
			ul = ul + '<li><img src="'+data[i].tbl+'" title="'+data[i].modif_name+'" width="100" height="75"></li>'
		}
		ul += '</ul>';
		$('#'+container).hide().html(ul).ready( function() { $('#'+container).show(); } );
		
		jQuery('#'+carousel_id).jcarousel({
//		visible: 2,
			scroll: 1,
			auto: 10,
//			animation: 'slow',
			wrap: 'circular',
			buttonNextHTML: null,
			buttonPrevHTML: null,
			initCallback: function( carousel ) {
				carousel.clip.toggle( function(){ carousel.stopAuto();}, function(){carousel.startAuto();});
			}
		});
	}
	}
// ---------------------------------------------------------------------------
})(jQuery);
